package communication.client;

import java.io.IOException;
import java.net.SocketException;

import communication.ServerConnectionHandle;
import communication.Message;
import communication.MessageFactory;
import communication.MessageType;
import communication.message.GameId;
import communication.message.Move;
import communication.message.PlayerLogin;
import exception.ParseException;

/***
 * @author Krzysztof Małaśnicki
 */
public class ServerHandle extends ServerConnectionHandle {

	public ServerHandle(String ip, int port) {
		super(ip, port);
	}
	
	public void disconnect() throws IOException {
		super.disconnect();
	}
	
	public void connect(PlayerLogin login) throws IOException {
		super.connect();
		Message msg=new Message(MessageType.PLAYER_LOGIN);
		msg.setPlayerLogin(login);
		super.send(msg);
	}
	
	public void sendMove(GameId gameId, Move move) throws IOException {
		Message msg=new Message(MessageType.MOVE);
		msg.setGameId(gameId);
		msg.setMove(move);
		super.send(msg);
	}
	
	public void finishGame(GameId id) throws IOException {
		Message msg=new Message(MessageType.THANK_YOU);
		msg.setGameId(id);
		super.send(msg);
	}
	
	
	public void leaveGame(GameId id) throws IOException {		
		Message msg=new Message(MessageType.LEAVE_GAME);
		msg.setGameId(id);
		super.send(msg);
	}	
	
	public Message receiveMessage() throws ParseException, SocketException {
		String s = super.receive();
		Message msg = null;
		msg = MessageFactory.fromString(s.trim());
		return msg;
	}
}
